// 1. 加载 express 模块
var express = require('express');
// 2. 创建一个 app 对象（类似于创建一个 server 对象）
var app = express();
//解析POST请求需要安装此依赖
//extended为false表示使用querystring来解析数据，这是URL-encoded解析器
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
// 3. 启动服务
app.listen(8082, function() {
    console.log('http://localhost:8082');
});
app.all('*', (req, res, next) => {
    console.log("-----all------");
    res.setHeader("Access-Control-Allow-Origin", "*");
    next();
})
const mysql = require('mysql');
const querySql = (sql, sqlParam, callback) => {
    var connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: '123456',
        port: '3306',
        database: 'book'
    });
    connection.connect();
    connection.query(sql, sqlParam, function(err, result) {
        callback(err, result)
    });
    connection.end();
}
app.get('/student/queryByName', (req, res) => {
    const sql = 'select * from user where name = ?'
    const sqlParam = [req.query.name1]
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            console.log('----------err', err);
            res.send(err);
        } else {
            console.log('----------result', result);
            res.send(result);
        }
    })

})
app.post('/student/queryByName', (req, res) => {
    console.log(req.body);
    const nowArray = studentList.filter((w) => w.name === req.body.name1);
    res.send(nowArray);
})